<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
<!--      <el-form-item label="所属课程" prop="platformId" v-if="!platformId">
        <el-select v-model="queryParams.platformId" placeholder="请选择课程" clearable>
          <el-option
            v-for="item in platformList"
            :key="item.baseId"
            :label="item.title"
            :value="item.baseId">
            <span style="float: left">{{ item.title }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">
              <dict-tag :options="dict.type.bmzt" :value="item.applyStatus"/>
            </span>
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="用户" prop="userName" v-if="!createBy">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入账户名或用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:userVideo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="data" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="baseId" v-if="false"/>
<!--      <el-table-column label="用户账户" align="center" prop="userName"/>-->
      <el-table-column label="用户名" align="center" prop="nickName"/>
      <el-table-column label="课程名称" align="center" prop="platformName"/>
      <el-table-column label="视频总时长" align="center" prop="totalTimeStr"/>
      <el-table-column label="观看总时长" align="center" prop="viewTimeStr"/>
      <el-table-column label="观看百分比(%)" width="300" align="center" prop="percent">
        <template slot-scope="scope">
          <el-progress :percentage="parseFloat(scope.row.percent)" text-color="#fff" :text-inside="true" :stroke-width="18"/>
        </template>
      </el-table-column>
      <el-table-column label="首次观看" align="center" prop="firstTime"/>
      <el-table-column label="最近观看" align="center" prop="lastTime"/>
      <el-table-column label="完成学习" align="center" prop="percent">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ywsf" :value="scope.row.finishStudy"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户视频进度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <div>
        <el-table
          :data="form"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          :stripe="true"
          border
          :default-expand-all="false">
          <el-table-column
            prop="name"
            label="名称"
            width="300">
          </el-table-column>
          <el-table-column
            prop="totalTimeStr"
            label="视频总时长">
          </el-table-column>
          <el-table-column
            prop="viewTimeStr"
            label="观看总时长">
          </el-table-column>
          <el-table-column
            prop="percent"
            width="300"
            label="观看百分比">
            <template slot-scope="scope">
              <el-progress :percentage="parseFloat(scope.row.percent)" :text-inside="true" :stroke-width="18"/>
            </template>
          </el-table-column>
          <el-table-column label="首次观看" align="center" prop="firstTime"/>
          <el-table-column label="最近观看" align="center" prop="lastTime"/>
          <el-table-column
            prop="points"
            label="获得积分数">
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUserVideo, getUserVideo, delUserVideo, addUserVideo, updateUserVideo} from "@/api/business/userVideo";
import {platformList} from "@/api/business/platform";

export default {
  name: "UserVideo",
  dicts: ['bmzt', 'ywsf'],
  props: {
    platformId: {
      type: String,
      default: ""
    },
    createBy: {
      type: String,
      default: ""
    },
    activeName: {
      type: String,
      default: ""
    },
  },
  watch: {
    activeName: {
      handler: function (val, oldVal) {
        this.queryParams.platformId = this.platformId;
        this.queryParams.userName = this.createBy;
        if (val === 'sixty') {
          this.getList();
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      // 课程列表
      platformList: [],

      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户视频进度表格数据
      data: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: this.createBy,
        platformId: this.platformId,
        videoName: undefined,
        content: undefined,
        totalTime: undefined,
        viewTime: undefined,
        percent: undefined,
      },
      form: [],
    };
  },
  created() {
    this.getList();
    if (!this.platformId) {
      this.getPlatformList();
    }
  },
  methods: {
    getPlatformList() {
      platformList().then(response => {
        this.platformList = response.data;
      });
    },
    /** 查询用户视频进度列表 */
    getList() {
      this.loading = true;
      listUserVideo(this.queryParams).then(response => {
        this.data = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.form=[];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.baseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      getUserVideo(row.userId, row.platformId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "用户视频进度";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('modules/userVideo/export', {
        ...this.queryParams
      }, `userVideo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
